Imports DAL
Imports System
Imports MyClasses
Imports System.Configuration

Public Class frmPurchaseReturn

    Dim TotAmount As Double
    Dim CPurInv As CPurchaseInvoice
    Dim ObjRetItems() As CReturnItem
    Dim intPurRecNo As Integer = 0
    Dim dtDItemDtl As DataTable
    Dim btLoad As Byte = 0
    Dim strItemTxt As String = ""
    Dim strBatchTxt As String = ""
    Dim strBillTxt As String = ""
    Dim strBilldateTxt As String = ""

    Private Sub frmPurchaseReturn_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub

    Private Sub frmPurchaseReturn_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If intPurRecNo > 0 Then
                    e.Cancel = False
                Else
                    If isUpdate() Then
                        Select Case BCommon.ShowMsgError(10081, "YNC")
                            Case Windows.Forms.DialogResult.Yes
                                Call SaveData()
                            Case Windows.Forms.DialogResult.No

                            Case Windows.Forms.DialogResult.Cancel
                                e.Cancel = True
                        End Select
                    End If
                End If
                intPurRecNo = 0
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmPurchaseReturn_FormClosing")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmPurchaseReturn_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Purchase Return")
        End If
    End Sub

    Private Sub frmPurchaseReturn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As DateTime = Now
        Call DisplayData()
        Call BCommon.SetFormStandards(Me)
        Call SetTag()
        MessageBox.Show((Now - dt).ToString)
    End Sub

    Private Sub SetTag()
        txtReceiptNo.Tag = txtReceiptNo.Text
        txtRemarks.Tag = txtRemarks.Text
        txtBatch.Tag = txtBatch.Text
        txtItemName.Tag = txtItemName.Text
        txtbill.Tag = txtbill.Text
        txtBillDate.Tag = txtBillDate.Text
    End Sub

    Private Sub DisplayData()
        Dim dtItems As DataTable
        Dim dtVendor As DataTable
        Try
            btLoad = 1
            Call ClearData()
            Call Grid_Settings()
            Call SetDatepickerMaxMinDate()
            txtReceiptNo.Text = BCommon.GetSequentialID("PR")
            BCommon.SetTextReadOnly(txtReceiptNo)
            BCommon.SetTextReadOnly(txtTotAmount)
            'Bind VendorName with ComboBox
            dtVendor = BVendor.GetVendor()
            If dtVendor.Rows.Count > 0 Then
                BCommon.SetComboValues(cmbVendor, dtVendor, "VendorID", "VendorName")
            End If
            'Bind Items with FlexGrid

            'Kiran@ITS on 20080421:For Performace Improvement.
            'dtItems = BAL.BSaleReturn.GetItem(0, True)
            'If dtItems.Rows.Count > 0 Then
            '    BCommon.SetComboValues(cmDItems, dtItems, "itemid", "itemname")
            'End If


            'Call SetBillIDs()
            Me.dtpRetDate.Value = Now.Date
            Me.lblRs.Text = ConfigurationManager.AppSettings("Currency").ToString()
            cmbVoucherNo.Text = ""
            intPurRecNo = 0
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    'COPY OF DIPKAYDATA 
    Private Sub InitialLoad()
        Dim dtItems As DataTable
        Dim dtVendor As DataTable
        Try
            btLoad = 1
            'Call ClearData()
            Call Grid_Settings()
            Call SetDatepickerMaxMinDate()
            txtReceiptNo.Text = BCommon.GetSequentialID("PR")

            'Bind VendorName with ComboBox
            dtVendor = BVendor.GetVendor()
            BCommon.SetComboValues(cmbVendor, dtVendor, "VendorID", "VendorName")

            'Bind Items with FlexGrid
            dtItems = BAL.BSaleReturn.GetItem(0, True)
            If dtItems.Rows.Count > 0 Then
                BCommon.SetComboValues(cmDItems, dtItems, "itemid", "itemname")
            End If


            'Call SetBillIDs()
            Me.dtpRetDate.Value = Now.Date
            Me.lblRs.Text = ConfigurationManager.AppSettings("Currency").ToString()
            cmbVoucherNo.Text = ""
            intPurRecNo = 0
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Private Sub Grid_Settings()
        Try
            btLoad = 1
            cfgRetItems.Clear(C1.Win.C1FlexGrid.ClearFlags.All)
            Call SetGridCaptions()
            Call SetColumnWidths()

            ''Common Settings
            With cfgRetItems
                .Cols(1).Editor = cmDItems
                .Cols(2).Editor = CmbBatchNo
                ' Hidden Columns
                .Cols(0).Visible = False        'Item ID
                .Cols(8).Visible = False        'STOCK(Available Qty)
                .Cols(9).Visible = False        'STOCKID
                .Cols(10).Visible = False        'TEMP RATE 
                .Cols(11).Visible = False        'PurItemQty  (Added By:-VINIT=2008/02/25)

                'EDITABLE/NON-EDITABLE COLUMNS
                .Cols(0).AllowEditing = False   'Item ID
                .Cols(1).AllowEditing = True    'ITEM NAME
                .Cols(2).AllowEditing = True    'BATCH
                .Cols(3).AllowEditing = False   'EXPIRY
                .Cols(4).AllowEditing = True    'REASON
                .Cols(5).AllowEditing = True    'QUANTITY
                .Cols(6).AllowEditing = True    'RATE    'Modified By:-VINIT(2008/01/22)
                .Cols(7).AllowEditing = False   'AMOUNT
                .Cols(8).AllowEditing = False   'STOCK
                .Cols(9).AllowEditing = False   'STOCKID
                'Added new field for bill rate Validation
                .Cols(10).AllowEditing = False   'TEMP RATE
                'Added new field for With bill for Return Quantity Validation
                .Cols(11).AllowEditing = False   'PurItemQty  (Added By:-VINIT=2008/02/25)

                .Cols(2).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.LeftCenter
                'DISPLAY FORMAT
                .Cols(3).DataType = System.Type.GetType("System.DateTime")
                .Cols(5).DataType = System.Type.GetType("System.Int32")
                .Cols(6).DataType = System.Type.GetType("System.Decimal")
                .Cols(7).DataType = System.Type.GetType("System.Decimal")
                .Cols(8).DataType = System.Type.GetType("System.Int32")
                .Cols(3).Format = ConfigurationManager.AppSettings("DateFormat").ToString()
                .Cols(10).DataType = System.Type.GetType("System.Decimal")
                .Cols(11).DataType = System.Type.GetType("System.Int32")

                .Cols(6).Format = "0.00"  '"M.R.P."
                .Cols(7).Format = "0.00"  '"BILL RATE"
            End With
            cfgRetItems.Rows.Count = 20
            dtpRetDate.Enabled = True
            cmbVendor.Enabled = True
            txtVendId.Enabled = True
            btnSave.Visible = True
            txtRemarks.Enabled = True
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Grid_Settings")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetGridCaptions()
        Try
            With cfgRetItems
                .Cols.Count = 12
                .Rows.Fixed = 1
                'CAPTIONS
                .Cols(0).Caption = "Item Id"
                .Cols(1).Caption = "Item Name"
                .Cols(2).Caption = "Batch No."
                .Cols(3).Caption = "Exp"
                .Cols(4).Caption = "Reason"
                .Cols(5).Caption = "Quantity"
                .Cols(6).Caption = "BillRate"
                .Cols(7).Caption = "Amount"
                .Cols(8).Caption = "Stock"
                .Cols(9).Caption = "StockId"
                .Cols(10).Caption = "TempRate"
                'PurItemQty  (Added By:-VINIT=2008/02/25)
                .Cols(11).Caption = "PurItemQty"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetGridCaptions")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetColumnWidths()
        Dim intGridWidth As Integer = cfgRetItems.Width - 5
        Try
            With cfgRetItems()
                .Cols(0).Width = 0    'Hidden Column         '"ITEM ID"
                .Cols(1).Width = intGridWidth / 11 * 3      '"ITEM NAME"
                .Cols(2).Width = intGridWidth / 7           '"BATCH"
                .Cols(3).Width = intGridWidth / 9           '"EXPIRY DATE"
                .Cols(4).Width = intGridWidth / 10           '"REASON"
                .Cols(5).Width = intGridWidth / 8           '"ITEM QUANTITY"
                .Cols(6).Width = intGridWidth / 8           '"BILL RATE"
                .Cols(7).Width = intGridWidth / 8           '"AMOUNT"
                .Cols(8).Width = 0    'Hidden Column         '"STOCK"
                .Cols(9).Width = 0    'Hidden Column         '"STOCKID"
                .Cols(10).Width = 0   'Hidden Column         '"TEMPRATE"
                .Cols(11).Width = 0   'Hidden Column         '"PurItemQty"
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetColumnWidths")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ClearData()
        Try
            btLoad = 1

            ChkSR.Checked = False
            ChkSR.Enabled = True
            txtBillDate.Enabled = False
            cmbVoucherNo.Enabled = False

            txtReceiptNo.Focus()
            txtReceiptNo.Text = String.Empty

            txtVendId.Text = String.Empty
            dtpRetDate.Value = Now.Date()
            dtpRetDate.Enabled = True

            cmbVendor.SelectedIndex = -1
            cmDItems.SelectedIndex = -1

            lblRs.Text = ConfigurationManager.AppSettings("Currency")
            txtTotAmount.Text = "0.00"
            txtRemarks.Text = String.Empty
            txtRemarks.Enabled = True
            txtItemName.Text = String.Empty
            txtItemName.Enabled = True
            txtBatch.Text = String.Empty
            txtBatch.Enabled = True

            'txtbill.Enabled = False
            'txtBillDate.Enabled = False

            cfgRetItems.Enabled = True
            cfgRetItems.Cols(1).Editor = Nothing
            cfgRetItems.Cols(2).Editor = Nothing

            cmDItems.Visible = False
            'cmDItems.DataSource = Nothing

            CmbBatchNo.Visible = False
            'CmbBatchNo.DataSource = Nothing

            'cmbVoucherNo.DataSource = Nothing
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SaveData()
        Try
            Dim intResult As Integer
            If ValidateData() = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            If ValidateInfo() = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            Call SetData()
            intResult = DPurchaseInvoice.SaveRetun("S", CPurInv, ObjRetItems, ChkSR.Checked)
            If intResult > 0 Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)
            If intResult > 0 Then
                'Divya@26/03/2007 : Changes done regarding Customize User Settings
                If ConfigurationManager.AppSettings("ViewReport") = "Yes" Then
                    If ConfigurationManager.AppSettings("CnfrmRepMsg") = "Yes" Then
                        If BCommon.ShowMsgError(10141, "YN") = Windows.Forms.DialogResult.Yes Then
                            Call ViewReport(intResult)
                        End If
                    End If
                End If
                'Call DisplayData()
                btLoad = 1
                Call ClearData()
                Call Grid_Settings()
                Call SetDatepickerMaxMinDate()
                txtReceiptNo.Text = BCommon.GetSequentialID("PR")
                BCommon.SetTextReadOnly(txtReceiptNo)
                BCommon.SetTextReadOnly(txtTotAmount)
                txtVendId.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ViewReport(ByVal intResult As Integer)
        'Try
        '    'If (MessageBox.Show(BCommon.GetMessage(10141), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
        '    Dim Report As New Reports.frmReportViewer
        '    With Report
        '        .strTitleName = "Purchase Return"
        '        .ReportName = "PurchaseReturn"
        '        ReDim .ParametersNames(0)
        '        ReDim .ParametersValues(0)
        '        .ParametersNames(0) = "PurReturnID"
        '        .ParametersValues(0) = intResult
        '        .ShowDialog()
        '    End With
        '    'End If
        'Catch ex As Exception
        '    BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewReport")
        '    Me.Cursor = Cursors.Default
        'End Try
    End Sub

    Private Sub SetData()
        Dim intItems As Integer
        Dim intCount As Integer
        Try
            With cfgRetItems
                For intCount = 1 To cfgRetItems.Rows.Count - 1
                    If cfgRetItems.Item(intCount, 0) <> Nothing OrElse Trim(cfgRetItems.Item(intCount, 0)) <> "" Then intItems = intItems + 1
                Next
                ReDim ObjRetItems(intItems - 1)
                For intCount = 0 To intItems - 1
                    ObjRetItems(intCount) = New CReturnItem
                    ObjRetItems(intCount).ItemID = .Item(intCount + 1, 0)
                    ObjRetItems(intCount).BatchNo = .Item(intCount + 1, 2)
                    ObjRetItems(intCount).Quantity = .Item(intCount + 1, 5)
                    ObjRetItems(intCount).Rate = .Item(intCount + 1, 6)
                    ObjRetItems(intCount).Amount = .Item(intCount + 1, 7)
                    ObjRetItems(intCount).Reason = .Item(intCount + 1, 4)
                Next
            End With
            CPurInv = New CPurchaseInvoice
            With CPurInv
                .InvoiceSeqID = txtReceiptNo.Text
                .InvoiceID = cmbVoucherNo.SelectedValue
                .InvoiceAmt = txtTotAmount.Text
                .VendorID = cmbVendor.SelectedValue
                If ChkSR.Checked = True And cmbVoucherNo.SelectedValue > 0 Then
                    .StockVendorID = cmbVoucherNo.Splits(0).DisplayColumns(5).DataColumn.Value
                Else
                    .StockVendorID = 0
                End If
                .BillDate = dtpRetDate.Value.Date
                .Remarks = txtRemarks.Text
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function ValidateData() As Byte
        Try
            If Trim(txtReceiptNo.Text) = String.Empty Then
                BCommon.ShowMsgError(10142)
                txtReceiptNo.Focus()
                Return False
            ElseIf BCommon.CheckSeqID(Trim(txtReceiptNo.Text), "PR") = True Then
                BCommon.ShowMsgError(10133)
                txtReceiptNo.Text = String.Empty
                txtReceiptNo.Focus()
                Return False
            End If
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub cmbVendor_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.DropDownClosed
        Try
            btLoad = 1
            If cmbVendor.SelectedIndex > 0 Then
                Me.txtVendId.Text = BCommon.GetSequentialCode("VENDOR") & Trim(Me.cmbVendor.SelectedValue.ToString())
            Else
                Me.txtVendId.Text = String.Empty
            End If
            If cmbVendor.SelectedIndex <> -1 Then
                If ChkSR.Checked = True Then Call SetBillIDs()
            End If
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendor_DropDownClosed")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgRetItems.AfterEdit
        If btLoad = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Try
            With cfgRetItems
                Select Case .Col
                    Case 1          'ItemName
                        If IsNothing(Trim(.Item(.Row, 1))) Or Trim(.Item(.Row, 1)) = "" AndAlso Not IsNothing(Trim(.Item(.Row, 2))) Or Trim(.Item(.Row, 2)) <> "" Then
                            .Rows.Remove(.Row)
                            .Rows.Count = .Rows.Count + 1
                        End If
                    Case 2          'Batch
                        If IsNothing(Trim(.Item(.Row, 1))) Or Trim(.Item(.Row, 1)) = "" AndAlso Not IsNothing(Trim(.Item(.Row, .Col))) Then
                            CmbBatchNo.DataSource = Nothing
                            cfgRetItems.Rows.Remove(e.Row)
                            cfgRetItems.Rows.Count = cfgRetItems.Rows.Count + 1
                            .Col = 1
                        End If
                        If Not IsNothing(Trim(.Item(.Row, 1))) AndAlso Trim(.Item(.Row, 1)) <> "" AndAlso Not IsNothing(Trim(.Item(.Row, .Col))) AndAlso Trim(.Item(.Row, .Col)) <> "" Then
                            Call SetItemRow()
                        Else
                            Call SetBlankItemRow(.Row)
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_AfterEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetItemRow()
        Dim intCount As Integer
        Try
            Dim intItemID As Integer ', StockID As Integer
            Dim strBatch As String = ""
            With cfgRetItems
                Select Case .Col
                    Case 2
                        'If cmDItems.SelectedValue > 0 And CmbBatchNo.Text <> "" Then
                        If cmDItems.SelectedValue > 0 Then
                            intItemID = cmDItems.SelectedValue
                            'StockID = dtDItemDtl.Rows(CmbBatchNo.SelectedIndex).Item(3)
                            strBatch = dtDItemDtl.Rows(CmbBatchNo.SelectedIndex).Item(2)
                        End If
                End Select
                If .Row > 0 Then
                    For intCount = 1 To .Rows.Count - 1
                        .Rows(.Row).Clear(C1.Win.C1FlexGrid.ClearFlags.Content)
                        'If StockID <> 0 Then
                        '    If .Item(intCount, 0) = intItemID And .Item(intCount, 9) = StockID Then
                        '        BCommon.ShowMsgError(10268)
                        '        .Col = 0
                        '        Exit Sub
                        '    End If
                        'End If
                        If strBatch <> "" Then
                            If .Item(intCount, 0) = intItemID And .Item(intCount, 2) = strBatch Then
                                BCommon.ShowMsgError(10268)
                                .Col = 0
                                Me.Cursor = Cursors.Default
                                Exit Sub
                            End If
                        End If
                    Next
                End If
                dtDItemDtl.PrimaryKey = New DataColumn() {dtDItemDtl.Columns(2)} 'Batch ID
                'Dim dtrItem As DataRow = dtDItemDtl.Rows.Find((StockID)) '(Items)
                Dim dtrItem As DataRow = dtDItemDtl.Rows.Find((strBatch)) '(Items)
                If IsNothing(dtrItem) Then Me.Cursor = Cursors.Default : Exit Sub
                .Item(.Row, 0) = dtrItem(0)                                'ItemID
                .Item(.Row, 1) = dtrItem(1)                                'ITEM NAME
                .Item(.Row, 2) = dtrItem(2)                                'BATCH
                .Item(.Row, 3) = dtrItem(4)                                'EXPIRY
                .Item(.Row, 4) = ""                                        'REASON
                If dtrItem(5) >= dtrItem(7) Then
                    .Item(.Row, 5) = dtrItem(7)                            'PurItemQty
                    .Item(.Row, 7) = dtrItem(7) * dtrItem(6)                   'AMOUNT
                Else
                    .Item(.Row, 5) = dtrItem(5)
                    .Item(.Row, 7) = dtrItem(5) * dtrItem(6)                   'AMOUNT
                End If
                .Item(.Row, 6) = dtrItem(6)                                'RATE
                .Item(.Row, 8) = dtrItem(7)                                'STOCK 
                .Item(.Row, 9) = dtrItem(3)                                'STOCKID
                .Item(.Row, 10) = dtrItem(6)                               'TEMP RATE 
                .Item(.Row, 11) = dtrItem(5)                               'PurItemQty 
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetBlankItemRow(ByVal intRow As Integer)
        Try
            With cfgRetItems
                '.Item(intRow, 0) = ""                                'ItemID
                '.Item(.Row, 1) = dtrItem(1)                                'ITEM NAME
                '.Item(intRow, 2) = ""                                'BATCH
                .Item(intRow, 3) = Nothing                                'EXPIRY
                .Item(intRow, 4) = Nothing                                        'REASON
                .Item(intRow, 5) = Nothing                               'QUANTITY
                .Item(intRow, 6) = Nothing                              'RATE
                .Item(intRow, 7) = Nothing                    'AMOUNT
                .Item(intRow, 8) = Nothing                               'STOCK
                .Item(intRow, 9) = Nothing                               'STOCKID
                .Item(intRow, 10) = Nothing                               'TEMP RATE 
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItemRow")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_BeforeDragRow(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.DragRowColEventArgs) Handles cfgRetItems.BeforeDragRow

    End Sub

    Private Sub cfgRetItems_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgRetItems.BeforeEdit
        If btLoad = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Dim intVendorId As Integer = 0
        Try
            If e.Col = 1 Then
                If e.Row > 1 AndAlso (IsNothing(cfgRetItems.Item(e.Row - 1, 0)) OrElse Trim(cfgRetItems.Item(e.Row - 1, 0)) = String.Empty) Then e.Cancel = True : Exit Sub 'PREVIOUS ROW IS FIELD
            End If
            If e.Col = 2 Then
                If e.Row > 1 AndAlso (IsNothing(cfgRetItems.Item(e.Row - 1, 0)) OrElse Trim(cfgRetItems.Item(e.Row - 1, 0)) = String.Empty) Then e.Cancel = True : Exit Sub 'PREVIOUS ROW IS FIELD
                If ChkSR.Checked = True Then
                    If cmbVoucherNo.SelectedIndex >= 0 Then
                        intVendorId = cmbVoucherNo.Splits(0).DisplayColumns(5).DataColumn.Value
                        dtDItemDtl = DPurchaseInvoice.GetReturnItemDtl(cmbVoucherNo.SelectedValue, cmDItems.SelectedValue, False, IIf(txtBatch.Text <> "", txtBatch.Text, ""), intVendorId)
                    End If
                Else
                    If cmDItems.SelectedIndex > 0 Then
                        dtDItemDtl = DPurchaseInvoice.GetReturnPRItem(cmDItems.SelectedValue, IIf(txtBatch.Text <> "", txtBatch.Text, ""))
                    End If
                End If

                If Not IsNothing(cfgRetItems.Item(e.Row, 1)) Then
                    If Not IsNothing(dtDItemDtl) Then
                        If dtDItemDtl.Rows.Count > 0 Then
                            CmbBatchNo.DataSource = dtDItemDtl
                            CmbBatchNo.ValueMember = "StockID"
                            CmbBatchNo.DisplayMember = "BatchNo"
                        Else
                            CmbBatchNo.DataSource = Nothing
                            Call SetBlankItemRow(e.Row)
                            btLoad = 0
                        End If
                    End If
                End If
            ElseIf e.Col > 3 AndAlso (Trim(cfgRetItems.Item(e.Row, 1)) = "" OrElse IsNothing(cfgRetItems.Item(e.Row, 1))) Then : e.Cancel = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_BeforeEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_BeforeRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RangeEventArgs) Handles cfgRetItems.BeforeRowColChange
        Try
            If btLoad = 1 Then Me.Cursor = Cursors.Default : Exit Sub
            If e.NewRange.r1 <> e.OldRange.r1 Then
                Me.CmbBatchNo.DataSource = Nothing
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_BeforeRowColChange")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_CellChanged(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgRetItems.CellChanged
        If btLoad = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Dim intCount As Integer
        Try
            TotAmount = 0.0
            With cfgRetItems
                For intCount = 1 To .Rows.Count - 1
                    If .Item(intCount, 5) = Nothing OrElse Trim(.Item(intCount, 5)) = "" OrElse CDbl(.Item(intCount, 5)) = 0.0 Then Me.Cursor = Cursors.Default : Exit For
                    TotAmount = TotAmount + .Cols(7).Item(intCount)
                Next
                txtTotAmount.Text = Format(TotAmount, "##,##0.00")
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_CellChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cfgRetItems.KeyDown
        Try
            If cfgRetItems.Col = 5 Then
                If e.KeyCode = Keys.Enter Then
                    txtRemarks.Focus()
                End If
            End If
            If e.KeyCode = Keys.Delete Then
                cfgRetItems.Rows.Remove(cfgRetItems.Row)
                cfgRetItems.Rows.Count = cfgRetItems.Rows.Count + 1
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtReceiptNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtReceiptNo.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                dtpRetDate.Focus()
            Else
                If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtReceiptNo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpRetDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpRetDate.KeyDown
        If e.KeyCode = Keys.Enter Then txtItemName.Focus()
    End Sub

    Private Sub txtRemarks_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRemarks.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub SetDatepickerMaxMinDate()
        Dim dtDatePkr As DataTable
        Try
            dtDatePkr = BCommon.GetDatePicker()
            dtpRetDate.MinDate = dtDatePkr.Rows(0).Item("fin_start_dt")
            dtpRetDate.MaxDate = dtDatePkr.Rows(0).Item("fin_end_dt")
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDatepickerMaxMinDate")
            Me.Cursor = Cursors.Default
        Finally
            dtDatePkr.Dispose()
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Call SaveData()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If btLoad = 1 Then Me.Cursor = Cursors.Default : Exit Sub
        Me.Close()
    End Sub

    Private Function isUpdate() As Boolean
        Try
            If Trim(txtItemName.Text) <> txtItemName.Tag _
            Or Trim(txtBatch.Text) <> Trim(txtBatch.Tag) Or Trim(txtRemarks.Text) <> Trim(txtRemarks.Tag) _
            Or Trim(txtbill.Text) <> Trim(txtbill.Tag) Or Trim(txtBillDate.Text) <> Trim(txtBillDate.Tag) Then
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        Try
            'Call DisplayData()
            btLoad = 1
            Call ClearData()
            Call Grid_Settings()
            Call SetDatepickerMaxMinDate()
            txtReceiptNo.Text = BCommon.GetSequentialID("PR")
            BCommon.SetTextReadOnly(txtReceiptNo)
            BCommon.SetTextReadOnly(txtTotAmount)
            txtVendId.Focus()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVendor.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                'If ChkSR.Checked = True Then 'Modified BY:-VINIT(2008/02/21)
                '    Call OnVendorChange()
                '    Call SetBillIDs()
                'End If
                If cmbVendor.SelectedIndex > 0 Then
                    Me.txtVendId.Text = BCommon.GetSequentialCode("VENDOR") & Trim(Me.cmbVendor.SelectedValue.ToString())
                Else
                    Me.txtVendId.Text = String.Empty
                End If
                Me.dtpRetDate.Focus()
            Else
                If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendor_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_LeaveCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgRetItems.LeaveCell
        ''If btLoad = 1 Then Exit Sub
        ''Try
        ''    'With cfgRetItems
        ''    '    If IsNothing(.Item(.Row, 5)) OrElse Trim(.Item(.Row, 5)) = "" Then
        ''    '        Exit Sub
        ''    '    Else
        ''    '        If CDbl(.Item(.Row, 5)) < 0.0 Then
        ''    '            BCommon.ShowMsgError(10114)
        ''    '            .Col = 5
        ''    '            .Item(.Row, 5) = .Item(.Row, 8)
        ''    '            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
        ''    '            Exit Sub
        ''    '        ElseIf CDbl(.Item(.Row, 5)) > CDbl(.Item(.Row, 8)) Then
        ''    '            BCommon.ShowMsgError(10143)
        ''    '            .Col = 5
        ''    '            .Item(.Row, 5) = .Item(.Row, 8)
        ''    '            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
        ''    '            Exit Sub
        ''    '        End If
        ''    '    End If
        ''    '    .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
        ''    'End With
        ''Catch ex As Exception
        ''    BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_LeaveCell")
        ''End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim form As New frmMasterBrowse
            form.strSearch = "Purchase Return"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            form.ShowDialog()
            intPurRecNo = form.strRecordNo
            If intPurRecNo > 0 Then
                Call ShowSearchRec(intPurRecNo)
                Call DisableControl()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmDItems_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmDItems.Enter
        Try
            If IsNothing(cmDItems.DataSource) Then
                Dim dtItems As DataTable
                dtItems = BAL.BSaleReturn.GetItem(0, True)
                If dtItems.Rows.Count > 0 Then
                    BCommon.SetComboValues(cmDItems, dtItems, "itemid", "itemname")
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDItems_Enter")
        End Try
    End Sub

    Private Sub cmDItems_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmDItems.KeyDown
        If e.KeyCode = Keys.Enter Then
            If ChkSR.Checked = True Then
                Call ItemChange()
                'Else
                '    Call SetBatch()
            End If
            CmbBatchNo.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtVendId_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVendId.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
    End Sub

    Private Sub txtVendId_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVendId.LostFocus
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.txtVendId.Text <> String.Empty Then
                dsVendorId = BVendor.GetVendorId(txtVendId.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmbVendor.SelectedValue = intVendId
                Else
                    BCommon.ShowMsgError(10219)
                    Me.txtVendId.Text = String.Empty
                    Me.txtVendId.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtVendId_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ShowSearchRec(ByVal intPurRecNo As Integer)
        Dim dtSearchRec As New DataTable
        Dim intCount As Integer
        Try
            dtSearchRec = DPurchaseInvoice.GetSearchItems(intPurRecNo)
            Call Grid_Settings()
            If dtSearchRec.Rows.Count > 0 Then
                If dtSearchRec.Rows.Count > 20 Then
                    cfgRetItems.Rows.Count = dtSearchRec.Rows.Count + 1
                ElseIf dtSearchRec.Rows.Count = 20 Then
                    cfgRetItems.Rows.Count = 21
                Else
                    cfgRetItems.Rows.Count = 20
                End If
                txtReceiptNo.Text = BCommon.GetSequentialCode("PR") & intPurRecNo
                txtVendId.Text = BCommon.GetSequentialCode("VENDOR") & dtSearchRec.Rows(0).Item("VendorID")
                cmbVendor.SelectedValue = dtSearchRec.Rows(0).Item("VendorID")
                If dtSearchRec.Rows(0).Item("InvoiceId") = 0 Then
                    ChkSR.Checked = False
                    cmbVoucherNo.Text = ""
                Else
                    ChkSR.Checked = True
                    cmbVoucherNo.Text = BCommon.GetSequentialCode("INVOICE") & dtSearchRec.Rows(0).Item("InvoiceId").ToString()
                End If
                dtpRetDate.Value = dtSearchRec.Rows(0).Item("ReturnDate")
                With cfgRetItems
                    For intCount = 0 To dtSearchRec.Rows.Count - 1
                        .Item(intCount + 1, 0) = dtSearchRec.Rows(intCount).Item("ItemID")                          'ItemID
                        .Item(intCount + 1, 1) = dtSearchRec.Rows(intCount).Item("itemname")                        'ITEM NAME
                        .Item(intCount + 1, 2) = dtSearchRec.Rows(intCount).Item("BatchNo")                         'BATCH
                        .Item(intCount + 1, 3) = dtSearchRec.Rows(intCount).Item("EXPDATE")                         'EXPIRY
                        .Item(intCount + 1, 4) = dtSearchRec.Rows(intCount).Item("Reason")                          'REASON
                        .Item(intCount + 1, 5) = dtSearchRec.Rows(intCount).Item("Qty")                             'QUANTITY
                        .Item(intCount + 1, 6) = dtSearchRec.Rows(intCount).Item("ReturnPrice")                     'RATE
                        .Item(intCount + 1, 7) = dtSearchRec.Rows(intCount).Item("Amount")                          'AMOUNT
                    Next
                End With
                txtRemarks.Text = dtSearchRec.Rows(0).Item("Remark")
            Else
                dtSearchRec = Nothing
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowSearchRec")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub DisableControl()
        Try
            cmbVendor.Enabled = False
            cmbVoucherNo.Enabled = False
            txtVendId.Enabled = False

            ChkSR.Enabled = False
            dtpRetDate.Enabled = False
            txtBatch.Enabled = False
            txtItemName.Enabled = False
            txtbill.Enabled = False
            txtBillDate.Enabled = False

            With cfgRetItems
                .Cols(1).AllowEditing = False               'Item Name
                .Cols(4).AllowEditing = False               'Reason
                .Cols(5).AllowEditing = False               'Qty
                .Enabled = False
            End With
            btnSave.Visible = False
            txtRemarks.Enabled = False
            txtReceiptNo.Focus()
            txtBillDate.Enabled = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisableControl")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgRetItems.SetupEditor
        Try
            With cfgRetItems
                Select Case .Col
                    Case 4  'REASON
                        Dim txt As TextBox = DirectCast(cfgRetItems.Editor, TextBox)
                        txt.MaxLength = 100
                    Case 5  'QTY
                        Dim txt As TextBox = DirectCast(cfgRetItems.Editor, TextBox)
                        txt.MaxLength = 6
                    Case 6 'BillRate
                        Dim txt As TextBox = DirectCast(cfgRetItems.Editor, TextBox)
                        txt.MaxLength = 13
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_SetupEditor")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgRetItems_ValidateEdit(ByVal sender As System.Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles cfgRetItems.ValidateEdit
        Try
            With cfgRetItems
                If e.Col = 6 Then
                    If cfgRetItems.Editor.Text <> "" Then
                        If CDbl(cfgRetItems.Editor.Text.ToString()) <= 0.0 Then
                            BCommon.ShowMsgError(10116)
                            cfgRetItems.Editor.Text = .Item(.Row, 10)
                            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
                            e.Cancel = True
                            Me.Cursor = Cursors.Default
                            Exit Sub
                        ElseIf CDbl(cfgRetItems.Editor.Text.ToString()) > CDbl(.Item(.Row, 10)) Then
                            BCommon.ShowMsgError(10342)
                            cfgRetItems.Editor.Text = .Item(.Row, 10)
                            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 10)
                            e.Cancel = True
                            Me.Cursor = Cursors.Default
                            Exit Sub
                        Else
                            .Item(.Row, 7) = .Item(.Row, 5) * CDbl(cfgRetItems.Editor.Text)
                            Me.Cursor = Cursors.Default
                            Exit Sub
                        End If
                    End If
                    '.Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
                End If

                If e.Col = 5 Then
                    If cfgRetItems.Editor.Text <> "" Then
                        If CDbl(cfgRetItems.Editor.Text) < 0.0 Then
                            BCommon.ShowMsgError(10114)
                            .Col = 5
                            If CDbl(.Item(.Row, 11)) >= CDbl(.Item(.Row, 8)) Then
                                cfgRetItems.Editor.Text = .Item(.Row, 8)                           'PurItemQty
                            Else
                                cfgRetItems.Editor.Text = .Item(.Row, 11)
                            End If
                            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
                            Me.Cursor = Cursors.Default
                            Exit Sub
                        ElseIf CDbl(cfgRetItems.Editor.Text) < 1 Then
                            BCommon.ShowMsgError(10225)
                            .Col = 5
                            If CDbl(.Item(.Row, 11)) >= CDbl(.Item(.Row, 8)) Then
                                cfgRetItems.Editor.Text = .Item(.Row, 8)                           'PurItemQty
                            Else
                                cfgRetItems.Editor.Text = .Item(.Row, 11)
                            End If
                            .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
                            Me.Cursor = Cursors.Default
                            Exit Sub
                        End If
                        If ChkSR.Checked = True Then
                            If CDbl(.Item(.Row, 11)) > CDbl(.Item(.Row, 8)) Then
                                If CDbl(cfgRetItems.Editor.Text) > CDbl(.Item(.Row, 8)) Then
                                    BCommon.ShowMsgError(10379)
                                    .Col = 5
                                    cfgRetItems.Editor.Text = .Item(.Row, 8)
                                    .Item(.Row, 7) = CDbl(cfgRetItems.Editor.Text) * .Item(.Row, 6)
                                    Me.Cursor = Cursors.Default
                                    Exit Sub
                                End If 'StockQty
                            Else
                                If CDbl(cfgRetItems.Editor.Text) > CDbl(.Item(.Row, 11)) Then
                                    BCommon.ShowMsgError(10143)
                                    .Col = 5
                                    cfgRetItems.Editor.Text = .Item(.Row, 11)
                                    .Item(.Row, 7) = CDbl(cfgRetItems.Editor.Text) * .Item(.Row, 6)
                                    Me.Cursor = Cursors.Default
                                    Exit Sub
                                End If 'PurItemQty
                            End If
                            .Item(.Row, 7) = CDbl(cfgRetItems.Editor.Text) * .Item(.Row, 6)
                        Else
                            If CDbl(cfgRetItems.Editor.Text) > CDbl(.Item(.Row, 8)) Then
                                BCommon.ShowMsgError(10379)
                                .Col = 5
                                cfgRetItems.Editor.Text = .Item(.Row, 8)
                                .Item(.Row, 7) = .Item(.Row, 5) * .Item(.Row, 6)
                                Me.Cursor = Cursors.Default
                                Exit Sub
                            End If 'StockQty
                            .Item(.Row, 7) = CDbl(cfgRetItems.Editor.Text) * .Item(.Row, 6)
                        End If
                    End If
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgRetItems_ValidateEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function ValidateInfo() As Boolean
        Dim intcount As Integer = 0
        Dim intFlag As Integer = 0
        Try
            For intCnt As Integer = 1 To cfgRetItems.Rows.Count - 1
                With cfgRetItems
                    If IsNothing(.Item(intCnt, 1)) OrElse Trim(.Item(intCnt, 1)) = "" Then Me.Cursor = Cursors.Default : CmbBatchNo.Focus() : Exit For
                    ' IS ITEM SELECTED 
                    If IsNothing(.Item(intCnt, 1)) OrElse Trim(.Item(intCnt, 1)) = "" Then BCommon.ShowMsgError(10230) : cmDItems.Focus() : Return False
                    'IS BATCH ENTERD
                    If IsNothing(.Item(intCnt, 2)) OrElse Trim(.Item(intCnt, 2)) = "" Then BCommon.ShowMsgError(10236) : CmbBatchNo.Focus() : Return False
                End With
            Next
            If cmbVendor.Text = "" Then
                BCommon.ShowMsgError(10261)
                'MessageBox.Show("Pelase Select Vendor Name", "Pharmacy Message")
                cmbVendor.Focus()
                Return False
            ElseIf DateTime.Compare(dtpRetDate.Value.Date, DateTime.Now()) > 0 Then
                BCommon.ShowMsgError(10228)
                dtpRetDate.Value = DateTime.Now()
                dtpRetDate.Focus()
                Return False
            ElseIf ChkSR.Checked = True And cmbVoucherNo.Text = "" Then
                BCommon.ShowMsgError(10117)
                'MessageBox.Show("Pelase Select Invoice ID", "Pharmacy Message")
                cmbVoucherNo.Focus()
                Return False
            ElseIf IsNothing(cfgRetItems.Item(1, 1)) = True Then
                BCommon.ShowMsgError(10230)
                cmDItems.Focus()
                Return False
            ElseIf IsNothing(cfgRetItems.Item(1, 2)) = True Then
                BCommon.ShowMsgError(10236)
                cmDItems.Focus()
                Return False
            ElseIf cfgRetItems.Rows.Count - 2 >= 0 Then
                For intcount = 0 To cfgRetItems.Rows.Count - 2
                    If cfgRetItems.Item(intcount + 1, 5) > 0.0 Or cfgRetItems.Item(intcount + 1, 5) <> Nothing Then
                        intFlag = 1
                    End If
                Next
                If intFlag = 1 Then
                    Return True
                Else
                    BCommon.ShowMsgError(10114)
                    cfgRetItems.Focus()
                    cfgRetItems.Col = 5
                    Return False
                End If
            Else
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub ChkSR_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkSR.CheckedChanged
        If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
            btLoad = 1
            Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
        End If
        If ChkSR.Checked = True Then
            cmDItems.DataSource = Nothing
            cfgRetItems.Cols(1).AllowEditing = False     'ITEM NAME
            cfgRetItems.Cols(2).AllowEditing = False     'BATCH
            cfgRetItems.Rows.Count = 20
            Call CheckedControl()
            Call SetBillIDs()
        Else
            cmbVoucherNo.Text = ""
            txtbill.Text = ""
            txtBillDate.Text = ""
            Call UnCheckedControl()
            Call SetItems()

            cfgRetItems.Cols(1).AllowEditing = True    'ITEM NAME
            cfgRetItems.Cols(2).AllowEditing = True    'BATCH
            Me.cfgRetItems.Focus()
            cfgRetItems.Row = 1
            cfgRetItems.Col = 1
            Me.cmDItems.Focus()
        End If
        btLoad = 0
    End Sub

    'Search the Record based on the Invoice Id
    Private Sub DisplayItems()
        Dim dtDItems As New DataTable
        Try
            Call ClearGridData()        'Remove The Data from Grid After New Vendor Is Selected
            If ChkSR.Checked = True Then
                If cmbVoucherNo.SelectedValue > 0 Then
                    dtDItems = DPurchaseInvoice.GetReturnItems(cmbVoucherNo.SelectedValue, False)
                End If
            End If

            If Not IsNothing(dtDItems) AndAlso dtDItems.Rows.Count > 0 Then
                If dtDItems.Rows.Count > 20 Then
                    cfgRetItems.Rows.Count = dtDItems.Rows.Count + 1
                ElseIf dtDItems.Rows.Count = 20 Then
                    cfgRetItems.Rows.Count = 21
                Else
                    cfgRetItems.Rows.Count = 20
                End If
                cmDItems.DataSource = dtDItems
                cmDItems.ValueMember = "ItemID"
                cmDItems.DisplayMember = "ItemName"
                cfgRetItems.Cols(1).AllowEditing = True    'ITEM NAME
                cfgRetItems.Cols(2).AllowEditing = True    'BATCH
                Me.cfgRetItems.Focus()
                cfgRetItems.Row = 1
                cfgRetItems.Col = 1
                Me.cmDItems.Focus()
            Else
                'BCommon.ShowMsgError(10381)
                cfgRetItems.Cols(1).AllowEditing = False    'ITEM NAME
                cfgRetItems.Cols(2).AllowEditing = False    'BATCH
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayItems")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Binding Bill ID based on the Vendor and Item 
    Private Sub SetBillIDs()
        Dim dtDItemList As New DataTable
        Try
            'dtDItemList = DPurchaseInvoice.GetPurChaseBillList(txtBillDate.Text.ToString(), IIf(txtItemName.Text <> "", Trim(txtItemName.Text), ""), IIf(txtBatch.Text <> "", Trim(txtBatch.Text), ""), IIf(txtbill.Text <> "", Trim(txtbill.Text), ""))
            dtDItemList = DPurchaseInvoice.GetPurChaseBillList(txtBillDate.Text.ToString(), IIf(txtItemName.Text <> "", Trim(txtItemName.Text), ""), IIf(txtBatch.Text <> "", Trim(txtBatch.Text), ""), IIf(txtbill.Text <> "", Trim(txtbill.Text), ""), cmbVendor.SelectedValue)
            If Not IsNothing(dtDItemList) Then
                If dtDItemList.Rows.Count > 0 Then
                    With cmbVoucherNo
                        .Text = String.Empty
                        .DataSource = dtDItemList
                        .Splits(0).DisplayColumns.Item(0).Visible = False
                        .Splits(0).DisplayColumns.Item(1).Width = .Width / 5
                        .Splits(0).DisplayColumns.Item(2).Width = .Width / 5
                        .Splits(0).DisplayColumns.Item(3).Width = .Width / 5
                        .Splits(0).DisplayColumns.Item(4).Width = (.Width / 5) * 2
                        .Splits(0).DisplayColumns.Item(5).Visible = False
                        .ValueMember = "InvoiceID"
                        .DisplayMember = "InvoiceNo"
                        Call ClearGridData()
                    End With
                Else
                    cmbVoucherNo.DataSource = Nothing
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetBillIDs")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbVoucherNo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVoucherNo.SelectedValueChanged
        Try
            If Not (cmbVoucherNo.SelectedValue = Nothing) OrElse Trim(cmbVoucherNo.SelectedValue) <> "" Then Call DisplayItems()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVoucherNo_SelectedValueChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Private Sub cmbVoucherNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVoucherNo.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                cmDItems.Focus()
                cfgRetItems.Row = 1
                cfgRetItems.Col = 1
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVoucherNo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Private Sub txtBillDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBillDate.KeyDown
        If e.KeyCode = Keys.Enter Then
            If isTextChange() Then
                Call SetBillIDs()
            End If
            cmbVoucherNo.Focus()
            Call GetText()
        End If
    End Sub

    Private Sub txtBillDate_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBillDate.LostFocus
        Try
            Dim TxtDate As String = Trim(txtBillDate.Text.ToString())
            If TxtDate <> String.Empty Then
                If Not TxtDate.Length < 8 Then
                    Dim dd As Integer = Mid(TxtDate, 1, 2)
                    Dim mm As Integer = Mid(TxtDate, 3, 2)
                    Dim yy As Integer = Mid(TxtDate, 5, 4)
                    If dd > 31 Or mm > 12 Or yy > 9999 Or yy < 1900 Then
                        BCommon.ShowMsgError(10305)
                        txtBillDate.Select(0, txtBillDate.TextLength)
                        txtBillDate.Focus()
                        Me.Cursor = Cursors.Default
                        Exit Sub
                    End If
                Else
                    BCommon.ShowMsgError(10305)
                    txtBillDate.Select(0, txtBillDate.TextLength)
                    txtBillDate.Focus()
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtBillDate_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    'Private Sub OnVendorChange()
    '    Dim dtItems As New DataTable
    '    Try
    '        If cmbVendor.SelectedIndex > 0 Then
    '            Me.txtVendId.Text = BCommon.GetSequentialCode("VENDOR") & Trim(Me.cmbVendor.SelectedValue.ToString())                
    '            dtItems = BSaleReturn.GetItem(cmbVendor.SelectedValue, True)
    '            'Binding Item Name
    '            If dtItems.Rows.Count > 0 Then
    '                cmbSearchItem.DataSource = Nothing
    '                cmbSearchItem.DataSource = dtItems
    '                cmbSearchItem.ValueMember = "ItemID"
    '                cmbSearchItem.DisplayMember = "ItemName"
    '            End If
    '            'Binding Batch Number
    '            If cmbSearchItem.SelectedIndex > 0 Then
    '                dtItems = DPurchaseInvoice.GetBatch(cmbSearchItem.SelectedValue, cmbVendor.SelectedValue)
    '            Else
    '                dtItems = DPurchaseInvoice.GetBatch("", cmbVendor.SelectedValue)
    '            End If
    '            If dtItems.Rows.Count > 0 Then
    '                cmbBatch.DataSource = Nothing
    '                cmbBatch.DataSource = dtItems
    '                cmbBatch.ValueMember = "BatchNo"
    '                cmbBatch.DisplayMember = "BatchNo"
    '            End If
    '        Else
    '            Me.txtVendId.Text = String.Empty
    '            dtItems = BAL.BSaleReturn.GetItem(0, True)
    '            If dtItems.Rows.Count > 0 Then
    '                cmbSearchItem.DataSource = Nothing
    '                cmbSearchItem.DataSource = dtItems
    '                cmbSearchItem.ValueMember = "ItemID"
    '                cmbSearchItem.DisplayMember = "ItemName"
    '            End If
    '            'Binding Batch Number
    '            If cmbSearchItem.SelectedIndex > 0 Then
    '                dtItems = DPurchaseInvoice.GetBatch(cmbSearchItem.SelectedValue, cmbVendor.SelectedValue)
    '                If dtItems.Rows.Count > 0 Then
    '                    cmbBatch.DataSource = Nothing
    '                    cmbBatch.DataSource = dtItems
    '                    cmbBatch.ValueMember = "BatchNo"
    '                    cmbBatch.DisplayMember = "BatchNo"
    '                End If
    '            Else
    '                dtItems = DItem.GetItemsBatch(0, True)
    '                If dtItems.Rows.Count > 0 Then
    '                    cmbBatch.DataSource = Nothing
    '                    cmbBatch.DataSource = dtItems
    '                    cmbBatch.ValueMember = "BatchNo"
    '                    cmbBatch.DisplayMember = "BatchNo"
    '                End If
    '            End If
    '        End If
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "OnVendorChange")
    '    End Try
    'End Sub

    'Search Item
    'Private Sub OnItemChange()
    '    Dim dtItems As New DataTable
    '    Try
    '        If cmbSearchItem.SelectedIndex > 0 Then
    '            If cmbVendor.SelectedIndex > 0 Then
    '                dtItems = DPurchaseInvoice.GetBatch(cmbSearchItem.SelectedValue, cmbVendor.SelectedValue)
    '            Else
    '                dtItems = DPurchaseInvoice.GetBatch(cmbSearchItem.SelectedValue, "")
    '            End If
    '            If dtItems.Rows.Count > 0 Then
    '                cmbBatch.DataSource = Nothing
    '                cmbBatch.DataSource = dtItems
    '                cmbBatch.ValueMember = "BatchNo"
    '                cmbBatch.DisplayMember = "BatchNo"
    '            End If
    '            Call SetBillIDs()
    '        Else
    '            dtItems = DItem.GetItemsBatch(0, True)
    '            If dtItems.Rows.Count > 0 Then
    '                cmbBatch.DataSource = Nothing
    '                cmbBatch.DataSource = dtItems
    '                cmbBatch.ValueMember = "BatchNo"
    '                cmbBatch.DisplayMember = "BatchNo"
    '            End If
    '        End If
    '    Catch ex As Exception
    '        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "OnItemChange")
    '    End Try
    'End Sub

    Private Sub ItemChange()
        'Dim dtItems As New DataTable
        'Try
        '    If cmDItems.SelectedIndex > 0 Then
        '        dtItems = DPurchaseInvoice.GetBatch(cmDItems.SelectedValue, "")
        '        If dtItems.Rows.Count > 0 Then
        '            CmbBatchNo.DataSource = Nothing
        '            CmbBatchNo.DataSource = dtItems
        '            CmbBatchNo.ValueMember = "BatchNo"
        '            CmbBatchNo.DisplayMember = "BatchNo"
        '        End If                
        '    Else
        '        dtItems = DItem.GetItemsBatch(0, True)
        '        If dtItems.Rows.Count > 0 Then
        '            CmbBatchNo.DataSource = Nothing
        '            CmbBatchNo.DataSource = dtItems
        '            CmbBatchNo.ValueMember = "BatchNo"
        '            CmbBatchNo.DisplayMember = "BatchNo"
        '        End If
        '    End If
        'Catch ex As Exception
        '    BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ItemChange")
        'End Try
    End Sub

    ''Private Sub SetBatch()
    ''    Dim dtBatch As New DataTable
    ''    Try
    ''        If cmDItems.SelectedIndex > 0 Then
    ''            dtBatch = DPurchaseInvoice.GetPRBatch(cmDItems.SelectedValue, IIf(txtBatch.Text <> "", txtBatch.Text, ""))
    ''            If dtBatch.Rows.Count > 0 Then
    ''                CmbBatchNo.DataSource = Nothing
    ''                CmbBatchNo.DataSource = dtBatch
    ''                CmbBatchNo.ValueMember = "BatchNo"
    ''                CmbBatchNo.DisplayMember = "BatchNo"
    ''            End If
    ''        End If
    ''    Catch ex As Exception
    ''        BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetBatch")
    ''    End Try
    ''End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    'Remove The Data from Grid After Clear Button Click
    Private Sub ClearGridData()
        Try
            btLoad = 1
            If Me.cfgRetItems.Rows.Count > 1 Then
                Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
                Me.cfgRetItems.Rows.Count = 20
                Me.txtTotAmount.Text = "0.00"
            Else
                Me.cfgRetItems.Rows.Count = 20
            End If
            btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearGridData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub



    Private Sub cmbVendor_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.Leave
        Try
            If Trim(cmbVendor.Text) = String.Empty Then
                txtVendId.Text = String.Empty
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendor_Leave")
            Me.Cursor = Cursors.Default
        End Try
    End Sub


    Private Sub cmDItems_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmDItems.SelectedValueChanged
        Try
            Call ItemChange()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmDItems_SelectedValueChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub UnCheckedControl()
        txtbill.Enabled = False
        txtBillDate.Enabled = False
        cmbVoucherNo.Enabled = False
    End Sub

    Private Sub CheckedControl()
        txtbill.Enabled = True
        txtBillDate.Enabled = True
        cmbVoucherNo.Enabled = True
    End Sub

    'Binding VoucherNo based on the Vendor and Item 
    'Private Sub SetVoucherNo()
    '    ''Dim dtDItemList As New DataTable
    '    ''Try
    '    ''    If cmbVendor.SelectedIndex > 0 Then
    '    ''        dtDItemList = DPurchaseInvoice.GetPurChaseBillList(cmbVendor.SelectedValue.ToString(), txtBillDate.Text.ToString(), IIf(cmbSearchItem.SelectedIndex > 0, cmbSearchItem.SelectedValue.ToString(), ""), IIf(cmbBatch.SelectedIndex > 0, cmbBatch.SelectedValue.ToString(), ""))
    '    ''    Else
    '    ''        dtDItemList = DPurchaseInvoice.GetPurChaseBillList("", txtBillDate.Text.ToString(), IIf(cmbSearchItem.SelectedIndex > 0, cmbSearchItem.SelectedValue.ToString(), ""), IIf(cmbBatch.SelectedIndex > 0, cmbBatch.SelectedValue.ToString(), ""))
    '    ''    End If
    '    ''    If Not IsNothing(dtDItemList) Then
    '    ''        If dtDItemList.Rows.Count > 0 Then
    '    ''            With cmbVoucherNo
    '    ''                .Text = String.Empty
    '    ''                .DataSource = dtDItemList
    '    ''                .Splits(0).DisplayColumns.Item(0).Visible = False
    '    ''                .Splits(0).DisplayColumns.Item(1).Width = .Width / 5
    '    ''                .Splits(0).DisplayColumns.Item(2).Width = .Width / 5
    '    ''                .Splits(0).DisplayColumns.Item(3).Width = .Width / 5
    '    ''                .Splits(0).DisplayColumns.Item(4).Width = (.Width / 5) * 2
    '    ''                .Splits(0).DisplayColumns.Item(5).Visible = False
    '    ''                .ValueMember = "InvoiceID"
    '    ''                .DisplayMember = "VoucherNo"

    '    ''                Call ClearGridData()                        'Remove The Data from Grid After New Vendor Is Selected
    '    ''            End With
    '    ''        Else
    '    ''            cmbVoucherNo.DataSource = Nothing
    '    ''        End If
    '    ''    End If
    '    ''Catch ex As Exception
    '    ''    BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetBillIDs")
    '    ''End Try
    'End Sub


    'Binding Items In FlexGrid
    Private Sub SetItems()
        Dim dtItems As New DataTable
        Try
            'If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
            '    btLoad = 1
            '    Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
            'End If
            dtItems = DPurchaseInvoice.GetPRItems(IIf(txtItemName.Text <> "", txtItemName.Text, ""), IIf(txtBatch.Text <> "", txtBatch.Text, ""))
            If dtItems.Rows.Count > 20 Then
                cfgRetItems.Rows.Count = dtItems.Rows.Count + 1
            ElseIf dtItems.Rows.Count = 20 Then
                cfgRetItems.Rows.Count = 21
            Else
                cfgRetItems.Rows.Count = 20
            End If
            If dtItems.Rows.Count > 0 Then
                BCommon.SetComboValues(cmDItems, dtItems, "ITEMID", "ITEMNAME")
            End If
            'btLoad = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetItems")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtItemName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtItemName.KeyDown
        If e.KeyCode = Keys.Enter Then
            If isTextChange() Then
                If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
                    btLoad = 1
                    Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
                End If
                If ChkSR.Checked = True Then
                    Call SetBillIDs()
                Else
                    Call SetItems()
                End If
                btLoad = 0
            End If
            Call GetText()
            txtBatch.Focus()
        End If
    End Sub

    Private Sub txtBatch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBatch.KeyDown
        If e.KeyCode = Keys.Enter Then
            If isTextChange() Then
                If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
                    btLoad = 1
                    Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
                End If
                If ChkSR.Checked = True Then
                    Call SetBillIDs()
                Else
                    Call SetItems()
                End If
                btLoad = 0
            End If
            Call GetText()
            If ChkSR.Checked = True Then
                txtbill.Focus()
            Else
                cfgRetItems.Focus()
                cfgRetItems.Row = 1
                cfgRetItems.Col = 1
                Me.cmDItems.Focus()
            End If
        End If
    End Sub

    Private Sub CmbBatchNo_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbBatchNo.DropDownClosed
        'If e.Col = 2 Then
        'If ChkSR.Checked = True Then
        '    If cmbVoucherNo.SelectedIndex >= 0 Then
        '        dtDItemDtl = DPurchaseInvoice.GetReturnItemDtl(cmbVoucherNo.SelectedValue, cmDItems.SelectedValue, False, IIf(txtBatch.Text <> "", txtBatch.Text, ""))
        '    End If
        'Else
        '    If cmDItems.SelectedIndex >= 0 And CmbBatchNo.SelectedIndex > 0 Then
        '        dtDItemDtl = DPurchaseInvoice.GetReturnItemDtl(0, cmDItems.SelectedValue, False, CmbBatchNo.SelectedValue)
        '    End If
        'End If

        'If Not IsNothing(cfgRetItems.Item(e.Row, 1)) Then
        '    If Not IsNothing(dtDItemDtl) Then
        '        If dtDItemDtl.Rows.Count > 0 Then
        '            CmbBatchNo.DataSource = dtDItemDtl
        '            CmbBatchNo.ValueMember = "StockID"
        '            CmbBatchNo.DisplayMember = "BatchNo"
        '        End If
        '    End If
        'End If
    End Sub

    Private Sub txtbill_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtbill.KeyDown
        If e.KeyCode = Keys.Enter Then
            If isTextChange() Then
                Call SetBillIDs()
            End If
            Call GetText()
            txtBillDate.Focus()
        End If
    End Sub

    Private Sub txtItemName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemName.LostFocus
        'If btLoad = 0 Then Exit Sub
        If isTextChange() Then
            If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
                btLoad = 1
                Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
            End If
            If ChkSR.Checked = True Then
                Call SetBillIDs()
            Else
                Call SetItems()
            End If
        End If
        Call GetText()
    End Sub

    Private Sub txtBatch_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBatch.LostFocus
        If isTextChange() Then
            If Not IsNothing(Me.cfgRetItems.Item(1, 1)) Then
                btLoad = 1
                Me.cfgRetItems.Rows.RemoveRange(1, Me.cfgRetItems.Rows.Count - 1)
            End If
            If ChkSR.Checked = True Then
                Call SetBillIDs()
            Else
                Call SetItems()
                cfgRetItems.Row = 1
                cfgRetItems.Col = 1
                Me.cmDItems.Focus()
            End If
        End If
        Call GetText()
    End Sub

    Private Sub txtbill_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtbill.LostFocus
        If isTextChange() Then
            Call SetBillIDs()
        End If
        Call GetText()
    End Sub


    Private Sub GetText()
        strItemTxt = txtItemName.Text
        strBatchTxt = txtBatch.Text
        strBillTxt = txtbill.Text
        strBilldateTxt = txtBillDate.Text
    End Sub

    Private Function isTextChange() As Boolean
        Try
            If Trim(txtItemName.Text) <> strItemTxt Or Trim(txtBatch.Text) <> strBatchTxt _
            Or Trim(txtbill.Text) <> strBillTxt Or Trim(txtBillDate.Text) <> strBilldateTxt Then
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function
End Class